package com.tool.finances.lifestyle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tool.finance.manager.IntentManager;

public class Setting extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		final ListView lv = (ListView) findViewById(R.id.ListView01);
		lv.setAdapter(new MyAdapter());
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent mIntent;
				if (position == 0) {
					mIntent = new Intent(Setting.this, CategorySetting.class);
					startActivity(mIntent);
				} else if (position == 1) {
					mIntent = new Intent(Setting.this, PasswordSetting.class);
					startActivity(mIntent);
				} else if (position == 2) {
					mIntent = new Intent(Setting.this, UnitSetting.class);
					startActivity(mIntent);
				}
			}
		});

	}

	private class MyAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if (null == view) {
				final LayoutInflater inflater = getLayoutInflater();
				view = inflater.inflate(R.layout.setting_item, null);
			}
			final ImageView ib = (ImageView) view
					.findViewById(R.id.ImageButton02SetCategory);
			final TextView tx = (TextView) view.findViewById(R.id.ConfigCategoryText);

			if (position == 0) {
				ib.setBackgroundResource(R.drawable.bg_category_setting);
				tx.setText("种类设置");
			} else if (position == 1) {
				ib.setBackgroundResource(R.drawable.bg_password_setting);
				tx.setText("密码设置");
			} else if (position == 2) {
				ib.setBackgroundResource(R.drawable.bg_unit_setting);
				tx.setText("Unit setting");
			}

			return view;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST + 1, 0, R.string.menu_help).setIcon(
				R.drawable.help);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		final boolean handle = true;
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			final IntentManager manager = new IntentManager(Setting.this);
			manager.gotoHelpActivity();
			break;
		default:
			break;
		}
		return handle || super.onOptionsItemSelected(item);
	}
}
